<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <!-- <el-button type="primary"  @click="dialogVisible = true" size="mini">添加分类</el-button> -->
        <!-- 添加分类 -->
        <el-button type="primary" @click="addVisible = true" size="mini"
          >添加分类</el-button
        >
      </div>

      <!-- 主体内容 -->
      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column
          prop="id"
          type="index"
          label="序号"
          width="100"
        ></el-table-column>
        <el-table-column prop="cate_name" label="分类名称"></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}" >
          <el-button type="primary" size="mini" @click="showEditDialog(row.id)"
            >修改</el-button
          >
          <el-button type="danger" @click="del(row.id)" size="mini">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加文章分类 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="30%"
      @closed="onAddClosed"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <!-- <el-button type="primary" @click="dialogVisible = false">确 定</el-button> -->
        <el-button type="primary" @click="addCate">添 加</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改文章分类" @closed="onEditClosed" :visible.sync="editVisible" width="30%">
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"  @click="editCate">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',

  data () {
    return {
      cateList: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称长度在 1 到 10 个字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名长度在 1 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取数据渲染
    async getCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      //   console.log(res)
      // 提示获取失败
      if (res.code !== 0) return this.$message.error(res.message)

      // 数据赋值给 cateList
      this.cateList = res.data
    },
    // 重置添加分类表单
    onAddClosed () {
      this.$refs.addForm.resetFields()
    },
    // 添加分类
    addCate () {
      this.$refs.addForm.validate(async (valid) => {
        if (!valid) return

        // 发起 ajax
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        // console.log(res)

        // 判断状态
        if (res.code !== 0) return this.$message.error(res.message)

        // 添加成功
        this.$message.success(res.message)

        // 添加成功后清空表单
        this.$refs.addForm.resetFields()

        // 关闭对话框
        this.addVisible = false

        this.getCateList()
      })
    },
    // 点击修改弹出层
    async showEditDialog (id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许修改此分类!')
      const { data: res } = await this.$http.get('/my/cate/info', { params: { id } })
      // console.log(res)

      // 判断状态
      if (res.code !== 0) return this.$message.error(res.message)

      // 赋值
      this.editForm = res.data

      // 开启弹出层
      this.editVisible = true
    },
    // 修改文章列表
    async editCate () {
      const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
      // console.log(res)

      // 判断状态
      if (res.code !== 0) return this.$message.error(res.message)

      // 修改成功提示
      this.$message.success(res.message)

      // 重新渲染
      this.getCateList()
      // this.$refs.editForm.resetFields()

      // 修改成功后关闭弹出层
      this.editVisible = false
    },
    // 关闭时重置修改表单
    onEditClosed () {
      this.$refs.editForm.resetFields()
    },
    // 删除
    del (id) {
      // 不允许删除前两个分类
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除此分类!')

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)

        // 重新渲染
        this.getCateList()
      }).catch(e => e)
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
